<template>
  <div>
    <div>
      <el-col :span="8" :offset="8">
        <el-tabs type="border-card" tab-position="bottom">
          <el-tab-pane label="登 录">
            <el-form label-width="80px" >
              <el-form-item label="用户名">
                <el-input v-model="login.username"></el-input>
              </el-form-item>
              <el-form-item label="密 码">
                <el-input v-model="login.password" type="password"></el-input>
              </el-form-item>
              <el-form-item>
                <el-button type="primary" @click="loginSubmit">登 录</el-button>
              </el-form-item>
            </el-form>
          </el-tab-pane>
          <el-tab-pane label="注 册">
            <el-form label-width="80px" >
              <el-form-item label="用户名">
                <el-input v-model="register.username"></el-input>
              </el-form-item>
              <el-form-item label="密 码">
                <el-input v-model="register.password" type="password"></el-input>
              </el-form-item>
              <el-form-item>
                <el-button type="primary" @click="registSubmit">注 册</el-button>
              </el-form-item>
            </el-form>
          </el-tab-pane>
        </el-tabs>
      </el-col>
    </div>
  </div>
</template>

<script>
  export default {
    name: "login",
    data() {
      return {
        user:{
          username:"",
          id:null
        },
        login:{
          username:"",
          password:""
        },
        register:{
          username:"",
          password:""
        },
      }
    },
    methods: {
      loginSubmit(){
        let _this = this
        axios.post("login",{
            username:this.login.username,
            password:this.login.password
          }).then((res)=> {
          res = res.data
          if (res.code == 200) {
            console.log(res.data)
            _this.user = res.data
            localStorage.setItem("userID",_this.user.id)
            localStorage.setItem("token",_this.user.token)
            console.log(localStorage.getItem("token"))
            this.$router.push({path:'/home'})
          }
        }).catch((err)=>(console.log(err)))
      },
      registSubmit(){
        let _this = this;
        axios.post("regist",{
          username:this.register.username,
          password:this.register.password
        }).then((res)=> {
          res = res.data
          if (res.code == 200) {
            _this.user = JSON.parse(res.data)
            localStorage.setItem("userID",_this.user.id)
            this.$router.push({path:'/home'})
          }
        }).catch((err)=>(console.log(err)))
      }
    }
  }
</script>

<style scoped>

</style>